package csel.view.dialog;


import java.awt.Color;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import csel.view.EquippableItemButton;
import csel.view.UseableItemButton;
import csel.controller.bindings.StringActionCommands;
import csel.model.Observer;
import csel.model.entity.Character;
import csel.model.items.EquippableItem;
import csel.model.items.ItemCollection;
import csel.model.items.TakeableItem;
import csel.model.items.UseableItem;

public class TradeDialog extends JPanel implements Observer
{
	//labels
	private JLabel topLabel;
	private JLabel pNameLabel;
	private JLabel npcNameLabel;
	//item button arrays
	private ArrayList<EquippableItemButton> pEIButtons = new ArrayList<EquippableItemButton>(10);
	private ArrayList<EquippableItemButton> npcEIButtons = new ArrayList<EquippableItemButton>(10);
	private ArrayList<UseableItemButton> pUIButtons = new ArrayList<UseableItemButton>(10);
	private ArrayList<UseableItemButton> npcUIButtons = new ArrayList<UseableItemButton>(10);
	//buttons
	private JButton buyButton = new JButton("Buy");
	private JButton sellButton = new JButton("Sell");
	private JButton closeButton = new JButton("Close");
	private ButtonGroup pItemButtonGroup = new ButtonGroup();
	private ButtonGroup npcItemButtonGroup = new ButtonGroup();
	//Panels
	private JPanel topPanel;
	private JPanel middlePanel;
	private JPanel botPanel;
	//Panels inside middlePanel
	private JPanel pPanel;
	private JPanel pPricePanel;
	private JPanel npcPanel;
	private JPanel npcPricePanel;
	private JPanel trdPanel;
	//Characters
	private Character pChar;
	private Character npcChar;	
	private ActionListener myActList;
	
	
	public static TradeDialog initialize(Character pChar, Character npcChar, ActionListener actionListener)
	{
		TradeDialog myTrade = new TradeDialog(pChar, npcChar, actionListener);
		pChar.addObserver(myTrade);
		npcChar.addObserver(myTrade);
		return myTrade;
	}
	
	private TradeDialog(Character pChar, Character npcChar, ActionListener actionListener){
		//set Characters
		this.npcChar = npcChar;
		this.pChar = pChar;
		myActList = actionListener;
		
		topLabel = new JLabel("Trade");
		topLabel.setSize(50, 50);
		pNameLabel = new JLabel(pChar.getName() + ": $"+pChar.getFlexBucks());
		pNameLabel.setSize(50, 50);
		npcNameLabel = new JLabel(npcChar.getName() + ": $"+npcChar.getFlexBucks());
		npcNameLabel.setSize(50, 50);
		//initialize button sizes
		buyButton.setSize(50, 50);
		buyButton.setActionCommand(StringActionCommands.BUY);
		buyButton.addActionListener(myActList);
		buyButton.setFocusable(false);
		
		sellButton.setSize(50, 50);
		sellButton.setActionCommand(StringActionCommands.SELL);
		sellButton.addActionListener(myActList);
		sellButton.setFocusable(false);
		
		closeButton.setSize(50, 50);
		closeButton.setActionCommand(StringActionCommands.HIDE_DIALOG);
		closeButton.addActionListener(myActList);
		closeButton.setFocusable(false);
		
		this.setBackground(Color.WHITE);
		initializeInventories();
		createPanels();
		setPanelSizes();
		setPanelLayouts();
		arrangePanels();
		fillPanels();
		
	}
	
	
	/**
	 * creates panels
	 */
	private void createPanels(){
		topPanel = new JPanel();
		topPanel.setBackground(Color.white);
		middlePanel = new JPanel();
		middlePanel.setBackground(Color.WHITE);
		botPanel = new JPanel();
		botPanel.setBackground(Color.WHITE);
		pPanel = new JPanel();
		pPanel.setBackground(Color.WHITE);
		npcPanel = new JPanel();
		npcPanel.setBackground(Color.WHITE);
		trdPanel = new JPanel();
		trdPanel.setBackground(Color.WHITE);
		npcPricePanel = new JPanel();
		npcPricePanel.setBackground(Color.WHITE);
		pPricePanel = new JPanel();
		pPricePanel.setBackground(Color.WHITE);
	}
	/**
	 * Set Panel sizes
	 */
	private void setPanelSizes(){
		this.setSize(400, 350);
		topPanel.setSize(400, 50);
		middlePanel.setSize(400, 200);
		botPanel.setSize(400, 50);
		pPanel.setSize(300, 200);
		npcPanel.setSize(300, 200);
		trdPanel.setSize(100, 200);
		npcPricePanel.setSize(100, 200);
		pPricePanel.setSize(100, 200);
	}
	private void arrangePanels(){
		this.add(topPanel);
		this.add(middlePanel);
		this.add(botPanel);
		middlePanel.add(pPanel);
		middlePanel.add(pPricePanel);
		middlePanel.add(trdPanel);
		middlePanel.add(npcPricePanel);
		middlePanel.add(npcPanel);
	}
	private void setPanelLayouts(){
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.LINE_AXIS));
		trdPanel.setLayout(new BoxLayout(trdPanel, BoxLayout.PAGE_AXIS));
		pPanel.setLayout(new BoxLayout(pPanel, BoxLayout.PAGE_AXIS));
		npcPanel.setLayout(new BoxLayout(npcPanel, BoxLayout.PAGE_AXIS));
		npcPricePanel.setLayout(new BoxLayout(npcPricePanel, BoxLayout.PAGE_AXIS));
		pPricePanel.setLayout(new BoxLayout(pPricePanel, BoxLayout.PAGE_AXIS));

	}
	
	public void mupdate(Object o, Object param)
	{
		if ((o.equals(pChar))||(o.equals(npcChar)))
		{
			/*
			this.removeAll();
			initializeInventories();
			createPanels();
			setPanelSizes();
			setPanelLayouts();
			arrangePanels();
			fillPanels();
			repaint();
			*/
		}
	}
	
	private void fillPanels(){

		pPanel.add(pNameLabel);
		npcPanel.add(npcNameLabel);
		for(EquippableItemButton eib : pEIButtons)
		{

			pPanel.add(eib);
			pPanel.add(new JLabel(eib.getPrice()));
		}
		
		for(EquippableItemButton eib : npcEIButtons)
		{

			npcPanel.add(eib);
			npcPanel.add(new JLabel(eib.getPrice()));
		}
		for(UseableItemButton uib : pUIButtons)
		{

			pPanel.add(uib);
			pPanel.add(new JLabel(uib.getPrice()));

		}
		for(UseableItemButton uib : npcUIButtons)
		{

			npcPanel.add(uib);
			npcPanel.add(new JLabel(uib.getPrice()));
		}
		
		topPanel.add(topLabel);
		trdPanel.add(buyButton);
		trdPanel.add(sellButton);
		trdPanel.add(closeButton);
	}

	
	public TakeableItem getBuyItem()
	{
		int size = npcEIButtons.size();
        for (int a = 0; a < size; ++a)
        {
                if (npcEIButtons.get(a).getSelectedObjects() != null)
                	return npcEIButtons.get(a).getEquippableItem();
        }
        
        size = npcUIButtons.size();
        for (int a = 0; a < size; ++a)
        {
                if (npcUIButtons.get(a).getSelectedObjects() != null)
                	return npcUIButtons.get(a).getUseableItem();
        }
        return null;
	}
	
	public TakeableItem getSellItem()
	{
		int size = pEIButtons.size();
		for (int a = 0; a < size; ++a)
		{
			if (pEIButtons.get(a).getSelectedObjects() != null)
				return pEIButtons.get(a).getEquippableItem();
		}

		size = pUIButtons.size();
		for (int a = 0; a < size; ++a)
		{
			if (pUIButtons.get(a).getSelectedObjects() != null)
				return pUIButtons.get(a).getUseableItem();
		}
		return null;
	}
	
	private void initializeInventories(){
		ItemCollection<EquippableItem> pEI    = pChar.getEquippableItems();
		ItemCollection<EquippableItem> npcEI  = npcChar.getEquippableItems();
		ItemCollection<UseableItem> pUI 	  = pChar.getUsableItems();			
		ItemCollection<UseableItem> npcUI     = npcChar.getUsableItems();
		for(int i =0; i < pEI.size(); i++)
		{
			EquippableItemButton but = new EquippableItemButton(pEI.getItem(i));
			but.setFocusable(false);
			pItemButtonGroup.add(but);
			pEIButtons.add(but); 
		}
		for(int i =0; i < npcEI.size(); i++)
		{
			EquippableItemButton but = new EquippableItemButton(npcEI.getItem(i));
			but.setFocusable(false);
			npcItemButtonGroup.add(but);
			npcEIButtons.add(but); 
		}
		for(int i =0; i < pUI.size(); i++)
		{
			UseableItemButton but = new UseableItemButton(pUI.getItem(i));
			but.setFocusable(false);
			pItemButtonGroup.add(but);
			pUIButtons.add(but); 
		}
		for(int i =0; i < npcUI.size(); i++)
		{
			UseableItemButton but = new UseableItemButton(npcUI.getItem(i));
			but.setFocusable(false);
			npcItemButtonGroup.add(but);
			npcUIButtons.add(but); 			
		}

	}

	
}
